<?php
namespace app\admin\behavior;

use app\admin\logic\AuthLogic;
use app\admin\util\Response;
use think\Request;
load_trait('controller/Jump');
class CheckAuth
{
    use \traits\controller\Jump;
    public function run(&$params)
    {
        //检验登录
        $adminInfo = session('admin');
        // print_r($adminInfo);
        $request = Request::instance();
        $this->_name = $request->controller();
        $this->_controller_name = $request->controller();
        if(empty($adminInfo)){//没登录的，检查当前动作是否需要登录
            $this->check_login();
        }
        if(!empty($adminInfo)){
            if(in_array($adminInfo['id'],config('founder_user_id'))){ //网站创始人
                define('FOUNDER',true);
            }else{
                define('FOUNDER', 0);
            }
            $userLogic = new AuthLogic();
            $flag = $userLogic->auth($request->module(),$request->controller(),$request->action());
            if(!$flag){
                if(Request::instance()->isAjax()){
                    Response::json(-1,'没有权限');
                }else{
                    $this->error('无此权限');
                }
            }
        }
    }
    private function check_login(){
        //白名单
        $white = array(
            'admin/login/index',
            'admin/admin/logout',
            'admin/admin/login',
        );
        if(empty($group) || empty($model) || empty($action)){
            $request = Request::instance();
            $group = strtolower($request->module());
            $model = strtolower($request->controller());
            $action = strtolower($request->action());
        }
        if(in_array(strtolower($group.'/'.$model.'/'.$action), $white)){
            return true;
        }else{
            $this->redirect(url('login/index'));
        }
    }
}



?>